<template>
  <s-layout title="进货" tabbar="/pages/index/cart" :bgStyle="{ color: '#fff' }">
    <s-empty
      v-if="state.list.length === 0"
      :text="$t('pages.cart.emptyText')"
      icon="/static/cart-empty.png"
    />

    <!-- 头部 -->
    <view class="cart-box ss-flex ss-flex-col ss-row-between" v-if="state.list.length">
      <view class="cart-header ss-flex ss-col-center ss-row-between ss-p-x-30">
        <view class="header-left ss-flex ss-col-center ss-font-26">
          {{$t('pages.cart.common')}}
          <text class="goods-number ui-TC-Main ss-flex ss-p-x-10">{{ state.list.length }}</text>
          {{$t('pages.cart.items')}}
        </view>
      </view>
      <!-- 内容 -->
      <view class="cart-content ss-flex-1 ss-p-x-30 ss-m-b-40">
        <view class="goods-box ss-r-10 ss-m-b-14" v-for="item in state.list" :key="item.id">
          <view class="ss-flex ss-col-center">
            <label class="check-box ss-flex ss-col-center ss-p-l-10" @tap="onSelectSingle(item.id)">
              <radio
			    :disabled="item.goods_num == 0"
                :checked="state.selectedIds.includes(item.id)"
                color="var(--ui-BG-Main)"
                style="transform: scale(0.8)"
                @tap.stop="onSelectSingle(item.id)"
              />
            </label>
            <s-goods-item
              :title="item.title"
              :img="item.image"
              :price="isArray(item.price) ? item.price[0] : item.price"
              :skuText="item.goods_sku_text"
              priceColor="#FF3000"
              :titleWidth="400"
              :dispatch_type_text="item.dispatch_type_text"
            >
              <template v-if="!state.editMode" v-slot:tool>
                <su-number-box
                  :min="0"
                  :max="item.stock"
                  :step="1"
                  v-model="item.goods_num"
                  @change="onNumberChange($event, item)"
                ></su-number-box>
              </template>
            </s-goods-item>
          </view>
        </view>
      </view>
	  
	  <uni-load-more
	    v-if="state.total > 0"
	    :status="state.loadStatus"
	    :content-text="{
	      contentdown: '上拉加载更多',
	    }"
	    @tap="loadmore"
	  />
      <!-- 底部 -->
      <su-fixed bottom :val="48" placeholder v-if="state.list.length > 0" :isInset="false">
        <view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
          <view class="footer-left ss-flex ss-col-center">
            <label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll">
              <radio
                :checked="state.isAllSelected"
                color="var(--ui-BG-Main)"
                style="transform: scale(0.8)"
                @tap.stop="onSelectAll"
              />
              <view class="ss-m-l-8"> {{$t('pages.cart.selectAll')}} </view>
            </label>
            <text>{{$t('pages.cart.total')}}：</text>
            <view class="text-price price-text">
              {{ state.totalPriceSelected }}
            </view>
          </view>
          <view class="footer-right">
            <button
              class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
              @tap="onConfirm"
            >
              去进货
              {{ state.selectedIds?.length ? `(${state.selectedIds.length})` : '' }}
            </button>
          </view>
        </view>
      </su-fixed>
    </view>
  </s-layout>
</template>

<script setup>
  import sheep from '@/sheep';
  import { computed, reactive, unref } from 'vue';
  import { isArray } from 'lodash';
  import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  
  const sys_navBar = sheep.$platform.navbar;
  const purchasing = sheep.$store('purchasing');

  const state = reactive({
    editMode: false,
    list: computed(() => purchasing.list),
    selectedList: [],
    total: computed(() => purchasing.total),
    loadStatus: computed(() => purchasing.loadStatus),
	
    selectedIds: computed(() => purchasing.selectedIds),
    isAllSelected: computed(() => purchasing.isAllSelected),
    totalPriceSelected: computed(() => purchasing.totalPriceSelected),
  });
  
  onLoad((options) => {
    purchasing.getList();
  });
  
  // 上拉加载更多
  onReachBottom(() => {
	  purchasing.loadmore();
  });

  // 单选选中
  function onSelectSingle(id) {
    purchasing.selectSingle(id);
  }
  // 全选
  function onSelectAll() {
    purchasing.selectAll(!state.isAllSelected);
  }

  // 结算
  function onConfirm() {
    let goods_list = [];
    state.selectedList = state.list.filter((item) => state.selectedIds.includes(item.id));
    state.selectedList.map((item) => {
      goods_list.push({
        goods_id: item.id,
        goods_num: item.goods_num,
        goods_sku_price_id: item.new_sku_prices[0].id
      });
    });
    if (goods_list.length === 0) {
      sheep.$helper.toast('请选择商品');
      return;
    }
	console.log(goods_list)
	sheep.$api.store.shopRestock({goods_list}).then(res => {
		sheep.$helper.toast('进货成功');
	})
  }

  function onNumberChange(e, cartItem) {
    if (e === 0) {
      purchasing.delete(cartItem.id);
      return;
    }
    if(cartItem.goods_num === e) return;
    cartItem.goods_num = e;
    purchasing.update({
      goods_id: cartItem.goods_id,
      goods_num: e,
      goods_sku_price_id: cartItem.goods_sku_price_id,
    });
  }
  async function onDelete() {
    purchasing.delete(state.selectedIds);
  }
</script>

<style lang="scss" scoped>
  :deep(.ui-fixed) {
    height: 72rpx;
  }

  .cart-box {
    width: 100%;

    .cart-header {
      height: 70rpx;
      background-color: #f6f6f6;
      width: 100%;
      position: fixed;
      left: 0;
      top: v-bind('sys_navBar') rpx;
      z-index: 1000;
      box-sizing: border-box;
    }

    .cart-footer {
      height: 100rpx;
      background-color: #fff;

      .pay-btn {
        width: 180rpx;
        height: 70rpx;
        font-size: 28rpx;
        line-height: 28rpx;
        font-weight: 500;
        border-radius: 40rpx;
      }
    }

    .cart-content {
      margin-top: 70rpx;
      .goods-box {
        background-color: #fff;
      }
    }
  }
</style>
